package com.naihe.anderson.sms.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.activity.SignatureMsgActivity;
import com.naihe.anderson.sms.entity.SignatureMsg;

public class SignatureMsgAdapter extends BaseAdapter {

	private Context mContext;
	private List<SignatureMsg> objects;
	private Handler mHandler;
	
	public SignatureMsgAdapter(Context context, List<SignatureMsg> objects, Handler handler){
		this.mContext = context;
		this.objects = objects;
		this.mHandler = handler;
	}
	
	@Override
	public int getCount() {
		if(objects == null) return 0;
		return objects.size();
	}

	@Override
	public SignatureMsg getItem(int position) {
		if(objects == null) return null;
		return objects.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = null;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            view = inflater.inflate(R.layout.signature_item_layout, null);
        } else {
            view = convertView;
        }
        
        SignatureMsg signatureMsg = objects.get(position);
        
        TextView signature_msg_tv = (TextView) view.findViewById(R.id.signature_msg_tv);
        signature_msg_tv.setText(signatureMsg.getBodyMsg());
//        signature_msg_tv.setMovementMethod(ScrollingMovementMethod.getInstance()) ; 
        Button info_statut = (Button) view.findViewById(R.id.info_statut);
        info_statut.setText((position+1) + "");
        Button sig_edit_btn = (Button) view.findViewById(R.id.sig_edit_btn);
        Button sig_send_btn = (Button) view.findViewById(R.id.sig_send_btn);
        Button sig_del_btn = (Button) view.findViewById(R.id.sig_del_btn);
        
        sig_edit_btn.setTag(signatureMsg);
        sig_send_btn.setTag(signatureMsg);
        sig_del_btn.setTag(signatureMsg);
        
        sig_edit_btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				SignatureMsg signatureMsg = (SignatureMsg) v.getTag();
				if(signatureMsg == null) return;
				
				Message message = mHandler.obtainMessage();
				message.obj = signatureMsg;
				message.what = SignatureMsgActivity.MSG_WHAT_EDIT;
				mHandler.sendMessage(message);
			}
		});
        sig_send_btn.setOnClickListener(new OnClickListener() {
        	
        	@Override
        	public void onClick(View v) {
        		SignatureMsg signatureMsg = (SignatureMsg) v.getTag();
				if(signatureMsg == null) return;
				
				Message message = mHandler.obtainMessage();
				message.obj = signatureMsg;
        		message.what = SignatureMsgActivity.MSG_WHAT_SEND;
        		mHandler.sendMessage(message);
        	}
        });
        sig_del_btn.setOnClickListener(new OnClickListener() {
        	
        	@Override
        	public void onClick(View v) {
        		SignatureMsg signatureMsg = (SignatureMsg) v.getTag();
				if(signatureMsg == null) return;
				
				Message message = mHandler.obtainMessage();
				message.obj = signatureMsg;
        		message.what = SignatureMsgActivity.MSG_WHAT_DELT;
        		mHandler.sendMessage(message);
        	}
        });
        
        if(signatureMsg.getSignatureMsgStatut() == SignatureMsg.SIGNATUREMSG_STATUT_COMPLETE){
        	info_statut.setBackgroundResource(R.drawable.signature_statut_send);
        	sig_send_btn.setVisibility(View.VISIBLE);
        } else if(signatureMsg.getSignatureMsgStatut() == SignatureMsg.SIGNATUREMSG_STATUT_SEND){
        	info_statut.setBackgroundResource(R.drawable.signature_statut_success);
        	sig_send_btn.setVisibility(View.GONE);
        } else {
        	info_statut.setBackgroundResource(R.drawable.signature_statut_error);
        	sig_send_btn.setVisibility(View.VISIBLE);
        }
        TextView info_time = (TextView) view.findViewById(R.id.info_time);
        info_time.setText(getDataFormat(signatureMsg.getSignatureMsgTime()));
        
        GridView grid_contact = (GridView) view.findViewById(R.id.grid_contact);
        SignatureGridAdapter gridAdapter = new SignatureGridAdapter(mContext, signatureMsg.getContactNumbers());
        grid_contact.setAdapter(gridAdapter);
        
        signatureMsg = null;
		return view;
	}
	
	private String getDataFormat(long time){
        String formatTime = "";
        Date date = new Date(time);
        try {
            Date nowDate = new Date();
            if(date.getYear()==nowDate.getYear() && date.getMonth()==nowDate.getMonth() && date.getDay()==nowDate.getDay()){
				formatTime = mContext.getResources().getString(R.string.today) + new SimpleDateFormat("HH:mm").format(date);
			}else{
				formatTime =  new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
			}
        } catch (Exception e) {
            formatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);;
        }
        return formatTime;
    }

}
